<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./vue.js"></script>
    <style>
        .menus {
            height: 42px;
        }

        .menus li {
            float: left;
            list-style: none;
            text-align: center;
            padding: 10px 20px;
            background: pink;
            cursor: pointer;
        }

        .menus li.active {
            background: teal;
            color: #fff
        }
    </style>
</head>

<body>
    <!-- 定义挂载点 -->
    <div id="app">
        <ul class="menus">
            <li v-for="(item,index) in menus" :key="item.id" :class="[ index == n ? 'active':'']" @click=" n = index ">
                {{item.title}}</li>
        </ul>
        <div class="comments" v-if="n==1">
            <input type="text" placeholder="昵称" v-model="user.name">
            <input type="text" placeholder="评论" v-model="user.text">
            <button @click="submit">立即提交</button>
        </div>
        <div class="showComments" v-else>
            <ul>
                <li v-for="item in msgs" :key="item.id" >
                    <h3>评论人：{{item.name}}</h3>
                    <h3>评论时间：{{item.date}}</h3>
                    <h3>评论内容:{{item.text}}</h3>
                </li>
            </ul>
        </div>
    </div>

    <script>
        new Vue({
            //挂载点
            el: "#app",
            //数据
            data: {
                n: 0, //表示当前选中的元素的下标
                menus: [
                    { title: "评论管理", id: 2 },
                    { title: "发表评论", id: 1 }
                ],
                user:{
                    name:"",
                    text:""
                },
                msgs:[]
            },
            //方法
            methods: {
                submit(){
                    this.msgs.push({
                        ...this.user,
                        id:new Date().getTime(),
                        date:new Date().toLocaleDateString()
                    });
                    console.log(this.msgs);
                }
            }
        })
    </script>
</body>

</html>